package org.microframework.base.core.redis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.TimeUnit;

/**
 * Redis分布式计数器服务
 */
@Slf4j
@Service
public class RedisCounterService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private static final String COUNTER_KEY_PREFIX = "counter:";

    /**
     * 创建或重置计数器
     *
     * @param key 计数器标识
     * @param initialValue 初始值
     * @param expireTime 过期时间
     * @param timeUnit 时间单位
     */
    public void setCounter(String key, long initialValue, long expireTime, TimeUnit timeUnit) {
        try {
            String redisKey = COUNTER_KEY_PREFIX + key;
            redisTemplate.opsForValue().set(redisKey, initialValue);
            if (expireTime > 0) {
                redisTemplate.expire(redisKey, expireTime, timeUnit);
            }
        } catch (Exception e) {
            log.error("设置计数器失败 - key: {}", key, e);
            throw e;
        }
    }

    /**
     * 计数器增加指定值
     *
     * @param key 计数器标识
     * @param increment 增加的值
     * @return 增加后的值
     */
    public long incrementBy(String key, long increment) {
        try {
            String redisKey = COUNTER_KEY_PREFIX + key;
            Long result = redisTemplate.opsForValue().increment(redisKey, increment);
            return result != null ? result : 0L;
        } catch (Exception e) {
            log.error("计数器增加失败 - key: {}", key, e);
            throw e;
        }
    }

    /**
     * 计数器减少指定值
     *
     * @param key 计数器标识
     * @param decrement 减少的值
     * @return 减少后的值
     */
    public long decrementBy(String key, long decrement) {
        try {
            String redisKey = COUNTER_KEY_PREFIX + key;
            Long result = redisTemplate.opsForValue().decrement(redisKey, decrement);
            return result != null ? result : 0L;
        } catch (Exception e) {
            log.error("计数器减少失败 - key: {}", key, e);
            throw e;
        }
    }

    /**
     * 获取计数器当前值
     *
     * @param key 计数器标识
     * @return 当前值
     */
    public long getCounter(String key) {
        try {
            String redisKey = COUNTER_KEY_PREFIX + key;
            Object value = redisTemplate.opsForValue().get(redisKey);
            if (value == null) {
                return 0L;
            }
            
            if (value instanceof Integer) {
                return ((Integer) value).longValue();
            } else if (value instanceof Long) {
                return (Long) value;
            }
            
            return Long.parseLong(value.toString());
        } catch (Exception e) {
            log.error("获取计数器值失败 - key: {}", key, e);
            return 0L;
        }
    }

    /**
     * 删除计数器
     *
     * @param key 计数器标识
     * @return 是否成功
     */
    public boolean deleteCounter(String key) {
        try {
            String redisKey = COUNTER_KEY_PREFIX + key;
            Boolean result = redisTemplate.delete(redisKey);
            return Boolean.TRUE.equals(result);
        } catch (Exception e) {
            log.error("删除计数器失败 - key: {}", key, e);
            return false;
        }
    }
} 